package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.DepartmentService;
import com.hdax.service.EmployeeInfoService;
import com.hdax.service.InterviewService;
import com.hdax.service.WagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 统计控制类
 */
@Controller
@RequestMapping("/tongji")
public class StatisticsController {

    @Autowired //员工
    private EmployeeInfoService employeeInfoService;

    @Autowired //部门
    private DepartmentService departmentService;

    @Autowired //工资
    private WagesService wagesService;

    @Autowired //面试
    private InterviewService interviewService;

    @GetMapping(path = "/index")
    public String index(Model model){
        Statistics ss = new Statistics();
        //获取部门数量
        List<Department> dt=departmentService.findAll();
        ss.setDepartcount(dt.size());
        //获取所有员工
        List<EmployeeInfo> ei=employeeInfoService.selectAll();
        ss.setEmpinfocount(ei.size());
        //获取所有正式员工
        ei= employeeInfoService.isnotnull();
        ss.setFormalcount(ei.size());
        //获取所有非正式员工
        ei=employeeInfoService.isnull();
        ss.setIsformalcount(ei.size());

        /**计算半年内的
         * 员工的数量
         * 每月发放工资的总额
         * 每月申请面试的人数
         */
        //获取本月时间
        Calendar cal=Calendar.getInstance();
        // 当前年
        int year = cal.get(Calendar.YEAR);
        // 当前月
        int month = cal.get(Calendar.MONTH) + 1;
        int daijian=5;
        List<Double> wage=new ArrayList<>();
        List<Integer> empinfo = new ArrayList<>();
        List<Integer> inter=new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            int xinmon=month-daijian;
            String date=null;
            if(xinmon<10){
                String xinjian="0"+xinmon;
                date=year+"-"+xinjian;
            }else{
                date=year+"-"+xinmon;
            }
            //本月的所有员工
            ei=employeeInfoService.setDate(date);
            empinfo.add(ei.size());
            //本月的总工资
            Wages w=wagesService.sumwage(date);
            if (w==null){
                wage.add(0.0);
            }else{
                wage.add(w.getShifamoney());
            }
            //本月面试的人数
            List<Interview> setTime=interviewService.setTime(date);
            inter.add(setTime.size());
            daijian--;
        }
        //员工数量变动
        ss.setEmpmapcount(empinfo);
        //工资变动
        ss.setWagescount(wage);
        //面试人数变动
        ss.setViewcount(inter);
        model.addAttribute("ss",ss);
        return "Statistics/StatisticsProfilres";
    }
}
